import React from 'react';
import { connect } from 'dva';
// import { message } from 'antd';
import router from 'umi/router';
import PageHeaderWrapper from '@/components/PageHeaderWrapper';
import BatchUpload from '../../components/BatchUpload/Index';
import StepItem from '../../components/BatchUpload/StepItem';
import FileUpload from '../../components/BatchUpload/Upload';
import Finish from '../../components/BatchUpload/Finish';
import BatchTaxInfo from './BatchTaxInfo';
import { urlParse } from '@/utils/utils';

@connect(({ batchsendns, publicdata, loading }) => ({
  uploading: loading.effects['publicdata/uploadFile'],
  batchQuery: batchsendns.batchQuery,
  fileId: publicdata.downloadFile.fileId,
  fileName: publicdata.downloadFile.fileName,
  currentStep: batchsendns.currentStep,
  progressPercent: batchsendns.progressPercent,
  resultInfo: batchsendns.resultInfo,
  showProgress: batchsendns.showProgress,
}))
class Batch extends React.PureComponent {
  componentDidMount() {
    const { dispatch, location } = this.props;
    const { wageFalg } = urlParse(location.search);
    dispatch({
      type: 'batchsendns/updateQuery',
      payload: { wageFalg, progressPercent: 0 },
    });
    dispatch({
      type: 'batchsendns/updateModelData',
      payload: {
        isGoBack: false,
      },
    });
  }

  componentWillUnmount() {
    this.clearModel();
  }

  clearModel = () => {
    const { dispatch } = this.props;
    dispatch({
      type: 'batchsendns/updateModelData',
      payload: {
        currentStep: 0,
        progressPercent: 0,
        showProgress: false,
        batchQuery: {
          // 批量上传数据
          progressPercent: 0,
          wageFalg: '2', // 导入类型	2、待发放。4、待报税、

          // 接单方待发放工资导入
          ordSndrId: undefined,
          custId: undefined,
          custName: undefined,
          incmType: undefined,
          busiYm: undefined,
          wageDate: undefined,
          changeFlag: '2', // 是否变更发放日期
          wageReaSendDate: undefined,
          reaRsn: undefined,
        },
      },
    });
  };

  // 设置当前位置
  setCurrentstep = currentStep => {
    const { dispatch } = this.props;
    dispatch({
      type: 'batchsendns/updateModelData',
      payload: { currentStep },
    });
    if (!currentStep) {
      dispatch({
        type: 'batchsendns/updateModelData',
        payload: {
          currentStep: 0,
          progressPercent: 0,
          showProgress: false,
        },
      });
    }
  };

  // 下一步
  onNext = () => {
    const { dispatch, currentStep } = this.props;
    dispatch({
      type: 'batchsendns/getDownloadInfoTax',
      payload: {},
    });
    this.setCurrentstep(currentStep + 1);
  };

  // 上传文件：需要携带参数即传：batchQuery
  uploadFile = formData => {
    const {
      batchQuery: { ordSndrId, custId, custName, incmType, busiYm },
      dispatch,
      resultInfo,
    } = this.props;
    const uploadQuery = {
      ordSndrId,
      custId,
      custName,
      incmType,
      busiYm,
    };
    // 需要携带参数即传：batchQuery，不需要传参可省略
    Object.entries(uploadQuery).forEach(([key, value]) => {
      formData.append(key, value);
    });
    dispatch({
      type: 'batchsendns/uploadFileWithProgress',
      payload: { formData, type: 'tax' },
    }).then(res => {
      if (res.success) {
        dispatch({
          type: 'batchsendns/updateModelData',
          payload: {
            showProgress: true,
            ...resultInfo,
            errorUrl: '/csApi/wageDowoload/dowoloadErrorTax', // 下载错误信息地址
            errorParams: null, // 下载错误数据参数
            // resultInfo: {
            //   // 结果数据必传
            //   batchName: '', // 任务名称
            //   taskCount: 0, // 导入数据
            //   successCount: 0, // 成功数据
            //   errorCount: 0, // 失败数据
            //   batchNo: '', // 批次号
            //   errorUrl: '/csApi/wageDowoload/dowoloadErrorTax', // 下载错误信息地址
            //   errorParams: null, // 下载错误数据参数
            // },
          },
        });
      }
    });
  };

  render() {
    const {
      fileId,
      fileName,
      uploading,
      batchQuery,
      location,
      dispatch,
      currentStep,
      showProgress,
      progressPercent,
      resultInfo,
    } = this.props;
    return (
      <PageHeaderWrapper>
        <BatchUpload currentStep={currentStep}>
          <StepItem title="选择工资信息">
            <BatchTaxInfo
              batchQuery={batchQuery}
              onNext={value => this.onNext(value)}
              location={location}
            />
          </StepItem>
          <FileUpload
            title="上传文件"
            currentStep={currentStep}
            setCurrentstep={this.setCurrentstep}
            downloadTempInfo={{
              // 下载模板信息
              params: { fileId, fileName },
              url: '/csApi/batchTask/downloadFile',
            }}
            uploadFile={this.uploadFile}
            uploading={uploading}
            // 展示进度条需要的数据
            progressPercent={progressPercent || 0}
            progressTitle="批量导入工资"
            progressVisible={showProgress}
            preStep={() => {
              this.setCurrentstep(currentStep - 1);
              dispatch({
                type: 'batchsendns/updateModelData',
                payload: {
                  isGoBack: true,
                },
              });
              router.push(`/salarymanage/salarysend/batchtax?wageFalg=${batchQuery.wageFalg}`);
            }}
          />
          <Finish title="完成" resultInfo={resultInfo} setCurrentstep={this.setCurrentstep} />
        </BatchUpload>
      </PageHeaderWrapper>
    );
  }
}

export default Batch;
